<?php

namespace App\Admin\Metrics;

use App\Admin\Repositories\OrgDepartmentOwner;
use App\Admin\Repositories\OrgTeamOwner;
use App\Admin\Repositories\TaskMemberMap;
use App\Admin\Repositories\TeamMember;
use App\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Metrics\SingleRound;
use Illuminate\Support\Facades\Auth;

class Member extends SingleRound
{
    public function init()
    {
        parent::init();

        $this->title('团队任务负载情况');
        $this->contentWidth(0, 12);
    }

    public function fill()
    {
        if (app(AdminController::class)->isAdmin()){
            $total_member_id_list = app(TeamMember::class)->getMemberIdList();
            $tasking_member_id_list = app(TaskMemberMap::class)->getMemberIdsFromTaskByTeams(\App\Models\MemberTask::TASK_STATUS_ING);
        }else{
            $team_id_list = [];
            $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
            if ($owner_department_list){
                $department_id_list = $owner_department_list->pluck('department_id')->toArray();
            }
            $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentIds(Auth::guard('admin')->id(), $department_id_list);
            if ($owner_team_list){
                $team_id_list = $owner_team_list->pluck('team_id')->unique()->toArray();
            }
            $total_member_id_list = app(TeamMember::class)->getMemberIdList($team_id_list);
            $tasking_member_id_list = app(TaskMemberMap::class)->getMemberIdsFromTaskByTeams(\App\Models\MemberTask::TASK_STATUS_ING);
            $tasking_member_id_list = $total_member_id_list->pluck('member_id')->intersect($tasking_member_id_list->pluck('member_id'));
        }

        $total_member_cnt = $total_member_id_list->count();
        $tasking_member_cnt = $tasking_member_id_list->count();
        if ($total_member_cnt == 0 || $tasking_member_cnt == 0){
            $this->withChart(0);
        }else{
            $this->withChart(ceil($tasking_member_cnt/$total_member_cnt*100));
        }

        $this->withFooter($total_member_cnt, $tasking_member_cnt);
    }

    public function getTotalMemberCnt() : int
    {

    }

    public function render()
    {
        $this->fill();

        return parent::render();
    }

    public function withChart($percent)
    {
        return $this->chart([
            'series' => [$percent],
        ]);
    }

    public function withFooter($completed, $inProgress)
    {
        return $this->footer(
            <<<HTML
<div class="row text-center mx-0" style="width: 100%">
  <div class="col-6 border-top border-right d-flex align-items-between flex-column py-1">
      <p class="mb-50">总人数</p>
      <p class="font-lg-1 text-bold-700 mb-50">{$completed}</p>
  </div>
  <div class="col-6 border-top d-flex align-items-between flex-column py-1">
      <p class="mb-50">任务中人数</p>
      <p class="font-lg-1 text-bold-700 mb-50">{$inProgress}</p>
  </div>
</div>
HTML
        );
    }
}